<?php
/**
 * Created by PhpStorm.
 * User: wuchunyin
 * Date: 2018/11/8
 * Time: 13:36
 */

namespace app\index\controller;
use app\index\base_controller\BaseController;
use app\index\table\RateInTable;
use app\index\table\RateOutTable;
use think\Db;

class Rate extends BaseController{

    /**
     * page 教育资源投入
     */
    public function rateInList(){
        return view('rate_in_list');
    }

    /**
     * action 教育资源投入
     */
    public function getRateInList(){
        $limit = input('limit');
        $offset = input('offset');
        $search = input('search');
        $sort = input('sort');
        $order = input('order');
        $sortStr = "$sort $order";
        if(empty($sort)){
            $sortStr = 'R_IN_ID desc';
        }

        $condition = null;
        if (!empty($search)) {
            $condition[] = ['SHORTNAME','like',"%$search%"];
        }

        try{
            $list = Db::table('V_RATE_IN_INFO')->where($condition)->order($sortStr)->limit($offset,$limit)->select();
            $total = Db::table('V_RATE_IN_INFO')->where($condition)->count();
            return ['rows'=>$list,'total'=>$total];
        }
        catch (Exception $e){
            return $e->getMessage();
        }
    }


    /**
     * page--新增教育资源投入页面
     */
    public function addPage(){
        $config = RateInTable::getConfigAdd();
        $this->assign('config',$config);
        $this->assign('data',null);
        $this->view->engine->layout('layout_popup');
        return view('rate_in_add');
    }

    /**
     * 新增教育资源投入指标
     */
    public function add() {
        $data = input();
        try{
            RateInTable::add($data);
            return json_encode(['code'=>'200','msg'=>'操作成功']);
        }
        catch (Exception $e){
            return json_encode(['code'=>'0','msg'=>'操作失败:'.$e->getMessage()]);
        }
    }

    /**
     * page--编辑页面--教育资源投入指标
     */
    public function editPage(){
        $rateInId = input('rateInId');
        $data = RateInTable::get($rateInId);
        if(empty($data)){
            return '查询教育资源投入指标信息失败';
        }
        $config = RateInTable::getConfigEdit();
        $this->assign('config',$config);
        $this->assign('data',$data);
        $this->view->engine->layout('layout_popup');
        return view('rate_in_edit');
    }

    /**
     * 编辑--教育资源投入指标
     */
    public function edit() {
        $rateInId = input('R_IN_ID');
        $data = input();
        try{
            RateInTable::update(['R_IN_ID'=>$rateInId],$data);
            return json_encode(['code'=>'200','msg'=>'操作成功']);
        }
        catch (Exception $e){
            return json_encode(['code'=>'0','msg'=>'操作失败:'.$e->getMessage()]);
        }
    }

    /**
     * page 导出Excel--教育资源投入指标
     */
    public function exportExcel(){
        $search = input('search');
        $sortStr = 'R_IN_ID desc';
        $condition = null;
        if (!empty($search)) {
            $condition[] = ['SHORTNAME','like',"%$search%"];
        }

        try{
            $list = RateInTable::table()->where($condition)->order($sortStr)->select();
            RateInTable::exportExcel($list,'教育资源投入指标');
        }
        catch (Exception $e){
            return $e->getMessage();
        }
    }

    /**
     * action 批量删除--教育资源投入指标
     */
    public function deleteAll(){
        $list = input('list');
        Db::startTrans();
        try{
            foreach ($list as $rateInId){
                RateInTable::delete(['R_IN_ID'=>$rateInId]);
            }
            Db::commit();
            return json_encode(['code'=>'200','msg'=>'操作成功']);
        }
        catch (Exception $e){
            Db::rollback();
            return json_encode(['code'=>'0','msg'=>'操作失败:'.$e->getMessage()]);
        }
    }

    /**
     * 打印 -- 教育资源投入指标
     */
    public function printInPage() {
        $id = input('id');
        $data = RateInTable::get($id);
        if(empty($data)){
            return '查询教育资源投入指标信息失败';
        }
        $config = RateInTable::getConfigPrint();
        $this->assign('config',$config);
        $this->assign('data',$data);
        $this->view->engine->layout('layout_popup');
        return view('rate_in_print');
    }


    /**
     * page--高级查询
     */
    public function searchPage(){
        $this->view->engine->layout('layout_popup');
        return view('book_search');
    }

    /**
     * page 教育资源产出
     */
    public function rateOutList(){
        return view('rate_out_list');
    }

    /**
     * action 教育资源产出
     */
    public function getRateOutList(){
        $limit = input('limit');
        $offset = input('offset');
        $search = input('search');
        $sort = input('sort');
        $order = input('order');
        $sortStr = "$sort $order";
        if(empty($sort)){
            $sortStr = 'R_OUT_ID desc';
        }

        $condition = null;
        if (!empty($search)) {
            $condition[] = ['SHORTNAME','like',"%$search%"];
        }

        try{
            $list = Db::table('V_RATE_OUT_INFO')->where($condition)->order($sortStr)->limit($offset,$limit)->select();
            $total = Db::table('V_RATE_OUT_INFO')->where($condition)->count();
            return ['rows'=>$list,'total'=>$total];
        }
        catch (Exception $e){
            return $e->getMessage();
        }
    }

    /**
     * page--新增教育资源产出页面
     */
    public function addRateOutPage(){
        $config = RateOutTable::getConfigAdd();
        $this->assign('config',$config);
        $this->assign('data',null);
        $this->view->engine->layout('layout_popup');
        return view('rate_out_add');
    }

    /**
     * 新增教育资源产出指标
     */
    public function addRateOut() {
        $data = input();
        try{
            RateOutTable::add($data);
            return json_encode(['code'=>'200','msg'=>'操作成功']);
        }
        catch (Exception $e){
            return json_encode(['code'=>'0','msg'=>'操作失败:'.$e->getMessage()]);
        }
    }

    /**
     * page--编辑页面--教育资源产出指标
     */
    public function editRateOutPage(){
        $rateOutId = input('rateOutId');
        $data = RateOutTable::get($rateOutId);
        if(empty($data)){
            return '查询教育资源产出指标信息失败';
        }
        $config = RateOutTable::getConfigEdit();
        $this->assign('config',$config);
        $this->assign('data',$data);
        $this->view->engine->layout('layout_popup');
        return view('rate_out_edit');
    }

    /**
     * 编辑--教育资源产出指标
     */
    public function editRateOut() {
        $rateOutId = input('R_OUT_ID');
        $data = input();
        try{
            RateOutTable::update(['R_OUT_ID'=>$rateOutId],$data);
            return json_encode(['code'=>'200','msg'=>'操作成功']);
        }
        catch (Exception $e){
            return json_encode(['code'=>'0','msg'=>'操作失败:'.$e->getMessage()]);
        }
    }

    /**
     * page 导出Excel--教育资源产出指标
     */
    public function exportRateOutExcel(){
        $search = input('search');
        $sortStr = 'R_OUT_ID desc';
        $condition = null;
        if (!empty($search)) {
            $condition[] = ['SHORTNAME','like',"%$search%"];
        }

        try{
            $list = RateOutTable::table()->where($condition)->order($sortStr)->select();
            RateOutTable::exportExcel($list,'教育资源产出指标');
        }
        catch (Exception $e){
            return $e->getMessage();
        }
    }

    /**
     * action 批量删除--教育资源产出指标
     */
    public function deleteRateOutAll(){
        $list = input('list');
        Db::startTrans();
        try{
            foreach ($list as $rateInId){
                RateOutTable::delete(['R_OUT_ID'=>$rateInId]);
            }
            Db::commit();
            return json_encode(['code'=>'200','msg'=>'操作成功']);
        }
        catch (Exception $e){
            Db::rollback();
            return json_encode(['code'=>'0','msg'=>'操作失败:'.$e->getMessage()]);
        }
    }

    /**
     * 打印 -- 教育资源产出指标
     */
    public function printOutPage() {
        $id = input('id');
        $data = RateOutTable::get($id);
        if(empty($data)){
            return '查询教育资源产出指标信息失败';
        }
        $config = RateOutTable::getConfigPrint();
        $this->assign('config',$config);
        $this->assign('data',$data);
        $this->view->engine->layout('layout_popup');
        return view('rate_out_print');
    }

    public function rateInImportPage() {
        $this->view->engine->layout('layout_popup');
        return view('rate_in_import');
    }

    public function rateInImport() {
        $inputData = input('data');
        $postData = base64_decode($inputData);
        $list = json_decode($postData,true);
        $start = input('start');

        Db::startTrans();
        $fieldList = RateInTable::RATE_IN_FIELD_LIST;
        $fieldCount = count($fieldList);
        foreach ($list as $key => $row) {
            $data = [];

            // 字段赋值
            $i = 0;
            foreach ($row as $key1=>$value) {
                $data[$fieldList[$i]] = $value;
                $i++;
                if ($i>=$fieldCount) {
                    break;
                }
            }

            try{
                RateInTable::add($data);
            }
            catch (Exception $e){
                Db::rollback();
                return json_encode(['code'=>'0','msg'=>"第".($key + $start)."行数据写入出错",'data'=>$e->getMessage()]);
            }
        }

        Db::commit();
        return json_encode(['code'=>'200','msg'=>'导入成功']);
    }


    public function rateOutImportPage() {
        $this->view->engine->layout('layout_popup');
        return view('rate_out_import');
    }

    public function rateOutImport() {
        $inputData = input('data');
        $postData = base64_decode($inputData);
        $list = json_decode($postData,true);
        $start = input('start');

        Db::startTrans();
        $fieldList = RateOutTable::RATE_OUT_FIELD_LIST;
        $fieldCount = count($fieldList);
        foreach ($list as $key => $row) {
            $data = [];

            // 字段赋值
            $i = 0;
            foreach ($row as $key1=>$value) {
                $data[$fieldList[$i]] = $value;
                $i++;
                if ($i>=$fieldCount) {
                    break;
                }
            }

            try{
                RateOutTable::add($data);
            }
            catch (Exception $e){
                Db::rollback();
                return json_encode(['code'=>'0','msg'=>"第".($key + $start)."行数据写入出错",'data'=>$e->getMessage()]);
            }
        }

        Db::commit();
        return json_encode(['code'=>'200','msg'=>'导入成功']);
    }



}